/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import Entities.Movie;
import Services.MovieService;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

/**
 *
 * @author sebastian
 */
public class MovieController {
    //Aqui van las funciones que reciben como parametros de la interfaz
    //las queries que se procesan y se devuelven a la vista
     EntityManagerFactory emf = Persistence.createEntityManagerFactory("GRLU");
     
    public String[] getAllMoviesNames(){
   
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        TypedQuery<Movie> query = (TypedQuery<Movie>) em.createNamedQuery("getAll", Movie.class);
        List<Movie> results = query.getResultList();
        int sizeMovies = results.size();
        int i = 0;
        String[] resultNames = new String[sizeMovies];
        for(Movie m: results ){
           resultNames[i] = m.getName();
           i++;
        }       
        return resultNames;                
    }
    
    public Movie getSingleMovie(String nameMovie){
   
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        TypedQuery query = (TypedQuery) em.createNamedQuery("getMovieByName", Movie.class);
        query.setParameter("name", nameMovie);
        Movie results = (Movie)query.getSingleResult();
        
        return results;                
    }
    
    public List<Movie> getMoviesbyName(String movieName){
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        TypedQuery<Movie> query = (TypedQuery<Movie>) em.createNamedQuery("getMovieByName", Movie.class);
        query.setParameter("name", movieName);
        List<Movie> resultsMovies = query.getResultList();        
        return resultsMovies;
    }
    
     public List<Movie> getMoviesbyGenre(String genreName){
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        TypedQuery<Movie> query = (TypedQuery<Movie>) em.createNamedQuery("Movie.findByGenre", Movie.class);
        query.setParameter("genre", genreName);
        List<Movie> resultsMovies = query.getResultList();        
        return resultsMovies;
    }
     
    public void removeSelectedMovie(String nameOfMovie){
         EntityManager em = emf.createEntityManager();
         em.getTransaction().begin();
         MovieService mS = new MovieService(em);
         TypedQuery<Movie> query = (TypedQuery<Movie>) em.createNamedQuery("getMovieByName");
         query.setParameter("name", nameOfMovie);
         //System.out.println(query.getResultList().get(0).getName());
         mS.removeMovie(query.getResultList().get(0).getId());      
         em.getTransaction().commit();
         
    }
    
    public void updateMovie(){
       
    }
    public void createMovie(String name, Integer year, String cover, String genre, String plotKeywords, Float rating, Float duration, Boolean availableForRental, Boolean availableForPurchase){
        EntityManager em = emf.createEntityManager();
        String[] numMovies = getAllMoviesNames();
        Integer numMo = numMovies.length;
                
        Movie newMovie = new Movie(Long.parseLong(numMo.toString()));
        
                                                        
        em.getTransaction().begin();
        MovieService mS = new MovieService(em);
        mS.createMovie(name, year, cover, genre, plotKeywords, rating, duration, availableForRental, availableForPurchase);
        
        em.flush();
        em.getTransaction().commit();
    }
    
    public List<Movie> getMoviesbyActor(String actorName){
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
//        TypedQuery<Movie> query = (TypedQuery<Movie>) em.createNamedQuery("getMovieByName", .class);
//        query.setParameter("name", movieName);
//        List<Movie> resultsMovies = query.getResultList();        
//        return resultsMovies;
        return null;
    }

    public void updateMovie(Long id, String text, Integer yearr, String cover, String text0, String text1, Float rating, float parseFloat, boolean selected, boolean selected0) {
      EntityManager em = emf.createEntityManager();
         em.getTransaction().begin();
         MovieService mS = new MovieService(em);
         mS.updateMovie(id, cover, yearr, cover, text1, cover, rating, rating, selected0, selected0);
         em.getTransaction().commit();
    }
    
}
